package com.baizhi.springbootshiro.config;

import com.alibaba.fastjson.JSON;
import com.baizhi.springbootshiro.utils.ContextBeanUtils;
import com.baizhi.springbootshiro.utils.JedisUtils;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;

import java.util.Collection;
import java.util.Set;

/**
 * 不适用于jedis
 *
 * @author Administrator
 */
public class CustomerShiroCache<K, V> implements Cache<K, V> {
    String cacheName;

    public CustomerShiroCache(String cacheName) {
        this.cacheName = cacheName;
    }

    @Override
    public V get(K k) throws CacheException {
        System.out.println("获取缓存");
        String s = getJedisUtils().hGet(cacheName, k.toString());
        Object parse = JSON.parse(s);
        return (V) parse;
    }

    @Override
    public V put(K k, V v) throws CacheException {
        System.out.println("存放缓存");
        Long aLong = getJedisUtils().hSet(cacheName, JSON.toJSONString(k), JSON.toJSONString(v));
        return (V) aLong;
    }

    @Override
    public V remove(K k) throws CacheException {
        Long aLong = getJedisUtils().hdel(cacheName, k.toString());
        return (V) aLong;
    }

    @Override
    public void clear() throws CacheException {

    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public Set<K> keys() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }

    public JedisUtils getJedisUtils() {
        return ContextBeanUtils.getBean("jedisUtils", JedisUtils.class);
    }


}
